import 'package:word_game/battle/kill_monster_event.dart';
import 'package:word_game/engine/game_event_bus.dart';

import 'task.dart';

/// 击杀怪物类型任务
/// code：怪物代码
/// number：怪物数量
/// 
class KillMonsterTask extends Task {
  List<Map<String, String>> monsterInfo = [];
  KillMonsterTask(String name) : super(name) {
    
  }

  @override
  void copyData(obj) {
    this.monsterInfo.addAll(obj.monsterInfo);
    super.copyData(obj);
  }

  @override
  accepted() {
    GameEventBus.instance.on<KillMonsterEvent>().listen((event){
      print('${this.receiver.name}接受的任务${this.name}：');
      print('${this.monsterInfo.toString()}');
      print('收到广播：${event.hero.name}击杀了${event.monster.name}');
      if (event.hero.id == this.receiver.id) {
        for (var info in monsterInfo) {
          if (info['code'] == event.monster.code) {
            int killedNumber = 0;
            if (info['killedNumber'] != null) {
              killedNumber = int.parse(info['killedNumber']);
            }
            killedNumber++;
            info['killedNumber'] = killedNumber.toString();
          }
        }
      }
      print('${this.monsterInfo.toString()}');
    });
    super.accepted();
  }
  
}